package org.csu.mypetstore.service;

import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.persistence.*;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@MapperScan
@EnableAutoConfiguration

public class CatalogService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private CartMapper cartMapper;


    public List<Category> getCategoryList() {
        return categoryMapper.getCategoryList();
    }

    public Category getCategory(String categoryId) {
        return categoryMapper.getCategory(categoryId);
    }

    public void deleteCategoryById(String categoryId){
        categoryMapper.deleteCategory(categoryId);
    }
    public void editCategory(Category category){
        categoryMapper.editCategory(category);
    }
    public Product getProduct(String productId) {
        return productMapper.getProduct(productId);
    }

    public void deleteProduct(String productId){
        productMapper.deleteProduct(productId);
    }
    public List<Product> getProductListByCategory(String categoryId) {
        return productMapper.getProductListByCategory(categoryId);
    }

    public List<Product> searchProductList(String keyword) {
        return productMapper.searchProductList("%" + keyword.toLowerCase() + "%");
    }


    public void updateItem(Item item){itemMapper.updateItem(item);}

    public void updateInventory(Item item){itemMapper.updateInventory(item);}

    public Item getItemById(String itemId){
        return itemMapper.findItemById(itemId);
    }

    public List<Item> getItemListByProduct(String productId) {
        return itemMapper.getItemListByProduct(productId);
    }

    public Item getItem(String itemId) {
        return itemMapper.getItem(itemId);
    }

    public void addItem(Item item){itemMapper.addItem(item);}


    public void deleteCart(String username,String itemId){
        cartMapper.deleteCart(username, itemId);
    }

    public List<CartItem> getCart(String username){
        List<CartItem> list=new ArrayList<>();

        List<ShoppingCart> list1 = new ArrayList<>();
        list1=cartMapper.getCart(username);
        if(!list1.isEmpty()) {
            for (ShoppingCart shoppingCart : list1) {
                CartItem cartItem = new CartItem();
                cartItem.setItem(getItem(shoppingCart.getItemId()));
                boolean isInStock=cartMapper.isInStock(shoppingCart.getItemId())>0;
                cartItem.setInStock(isInStock);
                cartItem.setQuantity(shoppingCart.getQuantity());
                list.add(cartItem);
            }
        }
        return list;
    }

    public boolean isItemInStock(String itemId) {
        return itemMapper.getInventoryQuantity(itemId) > 0;
    }
    public void store(ShoppingCart shoppingCart){

        cartMapper.store(shoppingCart);
    }
    public boolean isInStock(String id){
        return cartMapper.isInStock(id)>0;
    }
    public void beforeStore(String username){
        cartMapper.beforeStore(username);
    }

    public void insertProduct(String productid,String category,String name,String descn){productMapper.insertProduct(productid,category,name,descn);}
    public void insertItem(String itemid,String productid,String listprice,String unitcost, String attr1){itemMapper.insertItem(itemid,productid,listprice,unitcost,attr1);}
    public void deleteItem(String itemid){itemMapper.deleteItem(itemid);}
}
